﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

/// <summary>
/// OledbHelper 的摘要说明
/// </summary>
namespace SystemHelper
{   
  
    public class OledbHelper
    {
        private OleDbConnection MyConn;
        private string OledbStr;
        public OledbHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //   
            OledbStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source='"+HttpContext.Current.Server.MapPath("~/DB/DRSystem.mdb")+"'";
        }

        public void DBOpen()
        {
            MyConn = new OleDbConnection();
            MyConn.ConnectionString = OledbStr;
            if (MyConn.State == ConnectionState.Closed)
            {
                try
                {
                    MyConn.Open();
                }
                catch (Exception ex)
                {
                    MyConn = null;
                }
            }
        }

        public void DBClose()
        {
            if (MyConn != null)
            {
                if (MyConn.State == ConnectionState.Open)
                    MyConn.Close();
                MyConn.Dispose();
                MyConn = null;
            }
        }

        private OleDbCommand CreateSQLCommand(string cmdText)
        {
            DBOpen();
            OleDbCommand cmd = new OleDbCommand(cmdText, MyConn);
            return cmd;
        }

        private OleDbDataAdapter CreateSQLDataAdapter(string cmdText)
        {
            DBOpen();
            OleDbDataAdapter da = new OleDbDataAdapter(cmdText,MyConn);
            return da;
      }

        public int RunSQL(string cmdText)
        { OleDbCommand cmd = CreateSQLCommand(cmdText);
        int returnNum = 0;
        try
        { returnNum = cmd.ExecuteNonQuery(); }
        catch (Exception ex)
        { }
        finally
        { DBClose(); }
        return returnNum;
    }

        public void RunSQL(string cmdText,out OleDbDataReader dataReader)
        { OleDbCommand cmd = CreateSQLCommand(cmdText);
        try
        { dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); }
        catch (Exception ex)
        { dataReader = null; }
            
        }

        public void RunSQL(string cmdText, ref DataSet dataset)
        {
            if (dataset == null)
            { dataset = new DataSet(); }
            OleDbDataAdapter da = CreateSQLDataAdapter(cmdText);
            try
            { da.Fill(dataset); }
            catch (Exception ex)
            { }
            finally
            { DBClose(); }
        }

        public void RunSQL(string cmdText, ref DataSet dataset, int Min, int Max)
        {
            if (dataset == null)
            { dataset = new DataSet(); }
            OleDbDataAdapter da = CreateSQLDataAdapter(cmdText);
            try
            { da.Fill(dataset,Min,Max,"DATATABLE"); }
            catch (Exception ex)
            { }
            finally
            { DBClose(); }
        }
    }
}